Ext.define('Admin.view.main.ViewportController', {
	        extend : 'Ext.app.ViewController',
	        alias : 'controller.mainviewport',

	        listen : {
		        controller : {
			        '#' : {
				        unmatchedroute : 'onRouteUnmatch'
			        }
		        }
	        },

	        listeners : {
		        redirect : 'onRedirect'
	        },

	        routes : {
		        ':node' : 'onRouteMatch'
	        },

	        onRedirect : function(args) {
		        this.redirectTo(args.url);
	        },

	        setCurrent : function(view, routes) {
		        var me = this, container = me.getView(), containerLayout = container.getLayout();
		        var viewModel = me.getViewModel(), lastView = viewModel.get('currentView');

		        var newView, existedItem = container.child('component[routeId=' + view + ']');
		        if (!existedItem) {
			        newView = Ext.widget(view, {
				                hideMode : 'offsets',
				                routeId : view
			                });
		        }

		        if (!me.validateUser(newView || existedItem)) { // 认证用户是否登录
			        newView && newView.destroy();
			        return;
		        }

		        if (newView) {
			        Ext.suspendLayouts();
			        try {
				        containerLayout.setActiveItem(container.add(newView));
			        } finally {
				        Ext.resumeLayouts(true);
			        }
		        } else {
			        lastView = containerLayout.getActiveItem();
			        if (existedItem !== lastView) {
				        containerLayout.setActiveItem(existedItem);
			        }
			        newView = existedItem;
		        }

		        var controller = newView.getController(); // 继续切换下级页面
		        if (controller && controller.setCurrent) {
			        newView = controller.setCurrent(routes);
		        }

		        if (lastView && lastView.isWindow) { // 删除Window，不删除，会保留久值
			        container.remove(lastView);
			        lastView.destroy();
		        }

		        viewModel.set('currentView', newView);
		        return newView;
	        },

	        onRouteUnmatch : function(id) {
		        this.onRouteMatch(id);
	        },

	        onRouteMatch : function(id) {
		        var me = this, routes = id.split('/'), view = routes.shift();
		        return me.setCurrent(view, routes);
	        },

	        onLocationClick : function(btn) {
		        var win = btn.ownerCt.ownerCt.ownerCt, vm = win.getViewModel();
		        var record = vm.get('record'), city = vm.get('city').selection.data.name;
		        var ls = new BMap.LocalSearch(city);
		        ls.search(record.data.name || record.data.regName);
		        ls.setSearchCompleteCallback(function(rs) {
			                if (ls.getStatus() == BMAP_STATUS_SUCCESS) {
				                var poi = rs.getPoi(0);
				                record.data.lng = poi.point.lng;
				                record.data.lat = poi.point.lat;
			                }
		                });
	        },

	        privates : {
		        validateUser : function(view) { // 认证是否登录
			        var me = this, validate = Ext.isEmpty(view.validateUser) ? true : view.validateUser;

			        if (validate && Ext.isEmpty(me.getViewModel().get('user'))) {
				        return me.readUser();
			        }
			        return true;
		        },

		        readUser : function() {
			        var me = this, vm = me.getViewModel(), user = null;
			        Admin.Ajax.request({
				                url : 'rest/oauth/sign/info',
				                async : false,
				                fnSuccess : function(options, result) {
					                user = result.data;
					                vm.set('user', user);
					                // 设置默认的头像
					                if (Ext.isEmpty(user.avatar)) {
						                user.avatar = 'resources/images/icons/default-profile.png';
					                }
				                }
			                });
			        return user;
		        }
	        }

        });
